(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.wfBpmPkg.uploadVerifyController', uploadVerifyController);
	uploadVerifyController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'wfBpmPkgService' ];
	function uploadVerifyController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, wfBpmPkgService) {
		var ctrl = this, model = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.wfBpmPkg = {};
		ctrl.fileLoad = {
				loadId : model.dataKey,
				taskId : model.taskId
			};
		
		ctrl.init = function() {
			authService.uploadVerify(ctrl.fileLoad).then(function(response) {
				if (response.data) {
					ctrl.wfBpmPkg = response.data || [];
					ctrl.wfBpmPkg.bpmpkgName = ctrl.wfBpmPkg.bpmpkgName;
					ctrl.wfBpmPkg.artifactId = ctrl.wfBpmPkg.bpmpkgName.substring(0,9);
					ctrl.wfBpmPkg.taskId = ctrl.fileLoad.taskId;
				}
			});
		};

		ctrl.uploadVerifyReject = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.wfBpmPkg.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.uploadVerifyReject(ctrl.wfBpmPkg).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		ctrl.uploadVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.wfBpmPkg.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.uploadVerifyApprove(ctrl.wfBpmPkg).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.wfbpmpkg.list',
					'state' : 'cda.wfBpmPkg.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.wfbpmpkg.list',
					'state' : 'cda.wfBpmPkg.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
